package com.pine.manager.pineframework;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.pine.dao.pineframework.entity.SysRoleMenu;
import com.pine.dao.pineframework.mapper.SysRoleMenuMapper;
import com.pine.manager.core.BaseManager;
import com.pine.manager.pineframework.bo.SysRoleMenuBo;
import com.pine.manager.pineframework.convertors.SysRoleMenuConvertor;
import com.pine.manager.pineframework.query.SysRoleMenuQuery;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 角色菜单表 管理类
 * </p>
 *
 * @author pine manage
 * @since 2024-08-12
 */
@Component
public class SysRoleMenuManager extends BaseManager<SysRoleMenuMapper, SysRoleMenu, Long, SysRoleMenuBo, SysRoleMenuQuery, SysRoleMenuConvertor> {

    @Resource
    private SysRoleMenuConvertor sysRoleMenuConvertor;

    @Override
    public LambdaQueryWrapper<SysRoleMenu> encapsulateQueryWrapper(SysRoleMenuQuery query) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = super.encapsulateQueryWrapper(query);
        if (query == null) {
            return queryWrapper;
        }

        // 组装查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getRoleId()), SysRoleMenu::getRoleId, query.getRoleId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getMenuId()), SysRoleMenu::getMenuId, query.getMenuId());
        queryWrapper.in(ObjectUtils.isNotEmpty(query.getRoleIds()), SysRoleMenu::getRoleId, query.getRoleIds());

        return queryWrapper;
    }

    /**
     * 根据角色id删除角色菜单关联
     * @param roleId 角色id
     * @return 删除结果
     */
    public boolean deleteByRoleId(Long roleId) {
        return this.mapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId)) > 0;
    }
}
